
// 功能: 1)去除字符串前后所有空格
//      2)去除字符串中所有空格(包括中间空格,需要设置第2个参数为:g)
function trim(str,isGlobal){
  var result; 
  result = str.replace(/(^\s+)|(\s+$)/g,"");
  if(isGlobal.toLowerCase()=="g"){
    result = result.replace(/\s/g,"");
  }
  return result ;
}
//检查一个text值是否为空
function isEmptyText(obj){
  var value = trim(obj.attr("value"),'g');
  if(value == null || value == ''){
    return false;
  }
  return true;
}
//值是数字返回true
function checkNum(value){
  if(value == null || value == '' || isNaN(value)){
    return false;
  }
  return true;
}
//deate1小于date2返回true
function DateCompare(date1,date2){
  var  d1 = new Date(Date.parse(date1.replace(/-/g,"/")));  
  var d2 = new Date(Date.parse(date2.replace(/-/g,"/")));  
  if(d2 > d1)
  {
    return true;
  }else{
    return false;
  }
}


//新增或修改操作提交表单数据
function doSubmit(){
		$.ajax({
             url :$('form').first().attr("action"),  //后台处理程序
            type:'post',//数据发送方式
            data:$('form').first().serialize(),//要传递的数据；就是上面序列化的值
            success:function(result){
				if(0 == result.code){
					window.close();
					window.opener.reflash();
					window.opener=null;
				}else{
					if(!(result.errorMessage ==null || result.errorMessage == '')){
						alert(result.errorMessage);
					}
					//显示验证信息 把对象转为json数据
					var map = eval(result.data);
					$('label').text('');
					for(var k in map){
						$('#validateMessage_'+k).text(map[k]);
					}
				}
            },
            error:function (XMLHttpRequest, textStatus, errorThrown)
            {
            	alert(XMLHttpRequest.responseText);
            }
     }); 
}
	//新增或修改操作提交表单数据
function doSubmitforValidate(url){
		$.ajax({
             url :url,  //后台处理程序
            type:'post',//数据发送方式
            data:$('form').first().serialize(),//要传递的数据；就是上面序列化的值
            success:function(result){
				if(0 == result.code){
					window.close();
					window.opener.reflash();
					window.opener=null;
				}else{
					if(!(result.errorMessage ==null || result.errorMessage == '')){
						alert(result.errorMessage);
					}
					//显示验证信息 把对象转为json数据
					var map = eval(result.data);
					$('label').text('');
					for(var k in map){
						$('#validateMessage_'+k).text(map[k]);
					}
				}
            },
            error:function (XMLHttpRequest, textStatus, errorThrown)
            {
            	alert(XMLHttpRequest.responseText);
            }
     }); 
}
var popwin;
function openWin(URL,width,height){
		
		 var option="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes";
		 var w=500;
		 var h=500;
		 if(width){
		 	w=checkNum(width)?width:w;
		 }
		 if(height){
		 	h=checkNum(height)?height:h;
		 	
		 }
		 var l = (screen.width - w) / 2;
		 var t =(screen.height - h) / 2; 
         option+=",left="+l;
		 option+=",top="+t;
		 option+=",width="+w;
		 option+=",height="+h;
		 //alert(option);
		 if(popwin) popwin.close();
         popwin=window.open(URL,'',option);
}



function isShort(v){
	 var exp=/^\d+$/;	
	 return exp.test(v);
}
function trim(s) {
	return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}
function isNotEmpty(v){
     
	 return (v!=undefined && trim(v).length > 0);
}

function isNumber(val){
	var anum=/(^-?\d+$)|(^-?\d+\.\d+$)/;
	return anum.test(val);
}
function formSubmit(json,form){    
	addFromInputs(json , form);
	document.body.appendChild(form);
	form.submit();
}

function cloneForm(form){
	var newForm=document.createElement("form");
	newForm.innerHTML = form.innerHTML;
	return newForm;
}

function addFromInputs(json , form){
	if(json){
	        for( i   in   json){  
        	    var k=i;
	    		var v=json[i];
	    		if(v && v.constructor == Array){
		    		for(i=0;i<v.length;i++){
		    		    var input=document.createElement("input");
		    		    input.type="hidden";
		    		    input.name=k;
		    			input.value=v[i];
		    		    form.appendChild(input);
		    		    
		    		}
		    		
	    		}else{
		    		var input=document.createElement("input");
		    		input.type="hidden";
		    		input.name=k;
		    		input.value=v;
		    		form.appendChild(input); 
	    		}
            }
	    }
}


function jsonSubmit(json,url){
	   var form=document.createElement("form");
	   form.method="post";	       
	   addFromInputs(json , from)
	   document.body.appendChild(form);
	   form.action=url;
	   form.submit();
}

function top_tab_ctl(obj,m){
	var main="main";
	if(m){
		main=m;
	}
   	var rev=obj.title;
   	if(!(rev=="0" || rev=="1")){
   		rev="0";
   	}
   	
   	if(rev=="0"){
   		parent.document.getElementById(main).rows="100%,*";
   		obj.title="1";
   	}else{
   		parent.document.getElementById(main).rows="50%,*";
   		obj.title="0";
   	}
   		
 }
 
 function bottom_tab_ctl(obj,m){
	var main="main";
	if(m){
		main=m;
	}
   	var rev=obj.title;
   	if(!(rev=="0" || rev=="1")){
   		rev="0";
   	}
   	if(rev=="0"){
   		parent.document.getElementById(main).rows="*,100%";
   		obj.title="1";
   	}else{
   		parent.document.getElementById(main).rows="*,50%";
   		obj.title="0";
   	}   		
 }
 
 function test(){return ;}
    //添加
	function addRecord(strUrl,width,height){	
	   window.showModalDialog(_basePath+strUrl, window, "dialogWidth:"+width+"px; dialogHeight:"+height+"px; dialogHide:yes; scroll:no; status:no;toolbar=no;location=no");
	   window.location.href=window.location.href;
	}
	//修改
	function updateRecord(strUrl,width,height){	
		var allcheck = $("#userTable input:checked");
		if(allcheck.length == 0) {
			alert("请选择某一行");
			return;
		}
	    var id= "";
	    for(var i = 0;i<allcheck.length; i++){
		     id= allcheck[i].value;
	   }
	         strUrl= _basePath +strUrl+'?id='+id;
	    	window.showModalDialog(strUrl, window, "dialogWidth:"+width+"px; dialogHeight:"+height+"px; dialogHide:yes; scroll:no; status:no;toolbar=no;location=no");
	        window.location.href=window.location.href;
	}
	/**
	 * 修改角色权限信息
	 * @param url
	 */
	function alterRoleMenuRecord(url){
		var allcheck = $("#userTable input:checked");
		if(allcheck.length != 1) {
			alert("请选择某一行");
			return;
		}	
		window.location.href =_basePath +url+'?roleId='+allcheck[0].value;

	}
	
		//删除
		function deleteRecord(deleteUrl,pageListUrl){
			var allcheck = $("#userTable input:checked");
			if(allcheck.length == 0) {
				alert("请选择某一行");
				return;
			}
			var ids = "";
		    for(var i = 0;i<allcheck.length; i++){
				 var id =  allcheck[i].value;
			     ids = ids +","+id;
		   }
		$("#confirmDelete").dialog({
			height : "200",
			width : "200",
			modal : true,
			buttons : {
				"确定" : function() {
					$.post( _basePath +deleteUrl, {					
						ids : ids
					}, function(data) {
						$("#confirmDelete").dialog("destroy");
						showTipData(data,pageListUrl);	
					});
				},
				"取消" : function() {
					$(this).dialog("destroy");
				}
			},
			close : function() {
				$(this).dialog("destroy");
			}
		});	
		}
    
	function showTipData(data,pageListUrl){
		$("#showTips").empty();
		$("#showTips").html(data);
		$("#showTips").dialog({
		height : "220",
		width : "250",
		modal : true,
		buttons : {
			"关闭" : function() {
				$(this).dialog("destroy");
		        window.location.href =_basePath +pageListUrl;
			}
		},
		close : function() {
			$(this).dialog("destroy");
		}
	});
	}
	
	  function saveOk(){
		    var nodes = zTreeObj.getCheckedNodes(true);
		    var ids="";		   
		    for(var i=0;i<nodes.length;i++){
		     ids+=nodes[i].id+",";
		    }
		  $.post($("#authForm").attr("action"), {					
								menus:ids,roleId:$("#rID").val()
							}, function(data) {
							//	alert(data);
								showTips(data);
							},"html");
	  }
	  function reset1(){		 
		  zTreeObj.checkAllNodes(false);
		  $.ajax({ url: _basePath+ "/role/getRoleAuthsofRole.html", data:{roleId :$("#rID").val()},dataType:"json",context: document.body, success: function(data){	
				//alert(data.length);
				$.each(data, function(i, n) {		
					zTreeObj.expandAll(true);					
					var nodes = zTreeObj.transformToArray(zTreeObj.getNodes());
					//alert(nodes.length);
				    for(var i=1;i<=nodes.length;i++){
				    	var node = zTreeObj.getNodeByTId("tree_"+i);				    	
				    	if(node.id==n["menuId"]){
							node.checked=true ;
							zTreeObj.updateNode(node);
						}
				    }
				
				});
					
				},error:function (XMLHttpRequest, textStatus, errorThrown)
	            {
					showTips(XMLHttpRequest.responseText);
	            	
	            }});
		  
	  }
	  function showTips(data){
		  $("#noticeMsg").empty();
		  $("#noticeMsg").html(data);
		$("#noticeMsg").dialog({
		height : "230",
		width : "312",
		modal : true,
		buttons : {
			"关闭" : function() {
				$(this).dialog("destroy");
			}
		},
		close : function() {
			$(this).dialog("destroy");
		}
	});
				
  
  
		  
	  }
		 